from collections import OrderedDict

'''
构造自己的类字典对象
'''
class NoDupOrderedDict(OrderedDict):
    def __init__(self, clsname):
        self.clsname = clsname
        super().__init__()
    def __setitem__(self, name, value):
        if name in self:
            raise TypeError('{} already defined in {}'.format(name, self.clsname))
        super().__setitem__(name, value)

class OrderedMeta(type):
    def __new__(cls, clsname, bases, clsdict):
        d = dict(clsdict)
        d['_order'] = [name for name in clsdict if name[0] != '_']
        return type.__new__(cls, clsname, bases, d)

    @classmethod
    def __prepare__(cls, clsname, bases):
        return NoDupOrderedDict(clsname)
    


def recipe1():
    try:
        class A(metaclass=OrderedMeta):
            value1 = 'value1'
            value2 = 'value2'

            def spam(self):
                print('spam')
            def spam(self):
                print('spam')
        A()
    except TypeError as e:
        print(e)  

def main():
    print('recipe1'.center(20, '*'))
    recipe1()

if __name__ == '__main__':
    main()                    